/*
 *    Copyright 2008-2009 The Digester Tools Team*    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.annotations;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: DigesterLoader.java 357 2009-12-16 20:00:36Z simone.tripodi $
 */
public final class DigesterLoader {

    private static final DigesterLoader INSTANCE = new DigesterLoader();

    public static Digester createDigester(final Class<?> target) {
        return createDigester(target, new AnnotationsContext());
    }

    public static Digester createDigester(final Class<?> target, AnnotationsContext context) {
        return INSTANCE.createDigesterFrom(target, context);
    }

    public static void addRules(final Class<?> target, final Digester digester) {
        addRules(target, new AnnotationsContext(), digester);
    }

    public static void addRules(final Class<?> target, AnnotationsContext context, final Digester digester) {
        INSTANCE.addDigesterRules(target, context, digester);
    }

    public static RuleSet getRuleSet(final Class<?> target) {
        return getRuleSet(target, new AnnotationsContext());
    }

    public static RuleSet getRuleSet(final Class<?> target, AnnotationsContext context) {
        return INSTANCE.getRuleSetFrom(target, context);
    }

    private final Log log = LogFactory.getLog(this.getClass());

    private final Map<Class<?>, FromAnnotationsRuleSet> classRuleSetMap = new HashMap<Class<?>, FromAnnotationsRuleSet>();

    private DigesterLoader() {
        // hidden constructor
    }

    private Digester createDigesterFrom(final Class<?> target, AnnotationsContext context) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Creating new Digester to parse class '"
                    + target.getName()
                    + "'");
        }

        Digester digester = new Digester();
        digester.setClassLoader(target.getClassLoader());
        this.addDigesterRules(target, context, digester);
        return digester;
    }

    private void addDigesterRules(final Class<?> target, AnnotationsContext context, final Digester digester) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Adding rules to existing Digester to parse class '"
                    + target.getName()
                    + "'");
        }

        RuleSet ruleSet = this.getRuleSetFrom(target, context);
        ruleSet.addRuleInstances(digester);
    }

    private RuleSet getRuleSetFrom(final Class<?> target, AnnotationsContext context) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Getting Digester RuleSet from class '"
                    + target.getName()
                    + "'");
        }

        FromAnnotationsRuleSet ruleSet = null;

        if (this.classRuleSetMap.containsKey(target)) {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Class '"
                        + target.getName()
                        + "' previously analyzed, reusing existing rules");
            }

            ruleSet = this.classRuleSetMap.get(target);
        } else {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Analyzing '"
                        + target.getName()
                        + "' and caching produced RuleSet for future reuse");
            }

            ruleSet = new FromAnnotationsRuleSet();
            ruleSet.addRulesFrom(target, context);
            this.classRuleSetMap.put(target, ruleSet);
        }

        return ruleSet;
    }

}
